from rest_framework.request import Request
from core.config import TOKEN_MANAGE,attribute


def ex_request_headers(request, extracts: 'list|TOKEN_MANAGE',exclude:list=[], **kwargs):
    assert isinstance(extracts, (list, TOKEN_MANAGE)
                      ), "ex_request_headers parameter extracts type error."
    if 'headers' not in kwargs:
        kwargs['headers'] = {}
    if isinstance(extracts, list):
        for o in extracts:
            if not isinstance(o, str):
                raise TypeError('extracts list must [str].')
            if o not in request.META:
                raise  KeyError("%s not in request.META" % (o))
            if not exclude.__contains__(o):
                kwargs['headers'][o] = request.META[o]
    elif isinstance(extracts, TOKEN_MANAGE):
        for o in extracts.k_map.keys():
            if o.find('SAAS_', 0, 4) == 0:
                if o not in request.META:
                    raise KeyError("%s not in request.META" % (o))
                if not exclude.__contains__(o):
                    kwargs['headers'][o] = request.META[o]
    return kwargs

def ex_request_query_params(request, extracts:'list',exclude:list=[], **kwargs):
    assert isinstance(extracts, list), 'ex_request_params parameter extracts type error.'
    for o in extracts:
        if o not in request.query_params:
            raise KeyError("%s not in request.query_params"%(o))
        if not exclude.__contains__(o):
            kwargs[o]= request.query_params[o]
    return kwargs

def ex_request_data(request, **kwargs):
    kwargs['data']=request.data
    return kwargs    
